﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;


namespace UIKit
{
    /// <summary>
    /// http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html
    /// This class is not meant to be inherited from.
    /// It will need an actual view, in the form of a WPF user control
    /// </summary>
    public class UINavigationController : UIViewController
    {
        public UINavigationController initWithRootViewController(UIViewController rootViewController)
        {
            // TODO: implement in most simple form!
            try
            {
                this.pushViewController(rootViewController, false);
                return this;
            }
            catch {
                return null;
            }
        }

        #region properties
       
        public UIViewController topViewController{
            get {
                return viewControllers.Peek();
            }
        }
        public UIViewController visibleViewController;
        public Stack<UIViewController> viewControllers;

        public UINavigationBar navigationBar;  
        public bool navigationBarHidden ;

        public UIToolbar toolbar;
        public bool  toolbarHidden;
        public UINavigationControllerDelegate thedelegate;

        #endregion

        #region Methods

        public void setViewControllers(bool animated) { }
        public void setNavigationBarHidden(bool animated) { }
        public void setToolbarHidden(bool animated) { }
        
        /// <summary>
        /// This will be used by our list items to load their "detail" view.
        /// </summary>
        /// <param name="viewController">The view controller that is pushed onto the stack. This object cannot be an instance of tab bar controller and it must not already be on the navigation stack.</param>
        /// <param name="animated">Specify YES to animate the transition or NO if you do not want the transition to be animated. You might specify NO if you are setting up the navigation controller at launch time.</param>
        public void pushViewController(UIViewController viewController, bool animated) {
            transitionBetweenViewControllers(topViewController, viewController, animated, false);
            viewControllers.Push(viewController);
        }
        
        /// <summary>
        /// The back button will call this method (or the UIViewController?)
        /// </summary>
        /// <returns></returns>
        public UIViewController popViewControllerAnimated() {
            // TODO: Do instigate the animation somehow!
            // TODO: Dispose of the popped ViewCOntroller?
            // TODO: If you can't pop do nothing (per documentation);
            UIViewController fromUIController = viewControllers.Pop();
            transitionBetweenViewControllers(fromUIController, viewControllers.Peek(), true, true);
            return fromUIController;
        }

        public UIViewController[] popToRootViewControllerAnimated() {
            // TODO: implement
            return new UIViewController[] { new UIViewController() };
        }

        public UIViewController[] popToViewController(UIViewController viewController, bool animated) {
            // TODO: implement
            return new UIViewController[]{new UIViewController()};
        }

        #endregion

        #region non-UIKit code

        public UINavigationController()
        {
            //create the default view for the navigation, using embedded styles.
            this.initWithNibName("UIKit.UINavigationController_DEFAULTVIEW", null);
        }


        private void transitionBetweenViewControllers(UIViewController fromViewController, UIViewController toViewController, bool animated, bool backwards)
        {
            // TODO: Implement!
            if (thedelegate != null) thedelegate.willShowViewController(this, toViewController, animated);

            //make sure the view is loaded!
            if (!toViewController.isViewLoaded()) toViewController.loadView();

            if (animated)
            {
                // TODO: create the animation programmatically?
            }
            else
            {
                // TODO: where do i append the views to my sliding area (within this viewcontrollers view prop)
                fromViewController.view.viewElement.Visibility = Visibility.Collapsed;
                toViewController.view.viewElement.Visibility = Visibility.Visible;
            }

            UpdateNavigationBarAndButtons(toViewController);
            if (thedelegate != null) thedelegate.didShowViewController(this, toViewController, animated);
        }

        /// <summary>
        /// LG: Added for later
        /// </summary>
        private void UpdateNavigationBarAndButtons(UIViewController viewController)
        {
            // update back button
            // update title
            // update other buttons

            // navigationBar.update(viewController); ???!
        }

        #endregion

    }
}
